package top.csy8888.ch10.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * @author CSY
 * @date 2025 - 09 - 19
 */
@Configuration
/**
 * 启用Spring的消息代理(Broker)功能
 */
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    /**
     * 配置消息代理，用于订阅者和发布者使用
     * 通过传入的Broker注册器来注册一个Broker
     * @param registry Broker注册器
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        //创建Spring内置的简单消息队列并设置一个主题前缀（可以同时设置多个）
        // 用于消息的订阅和发布
        registry.enableSimpleBroker("/news","/sports");
    }

    /**
     * 注册STOMP的连接端点
     * 不再是配置WebSocket的连接端点
     * @param registry
     */
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("message-broker");
    }
}
